<?php

namespace app\shop\controller\business;

use think\Controller;

class Base extends Controller
{
    // 用户模型
    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();
        // 加载模型
        $this->BusinessModel = model('business.Business');
    }

    public function register()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $password = $this->request->param('password', '', 'trim');

        if (!$password) {
            $this->error('请输入密码');
        }

        $salt = mt_rand();
        $password = md5($password . $salt);

        // 封装数据
        $data = [
            'mobile' => $mobile,
            'password' => $password,
            'salt' => $salt,
            'deal' => 0,
            'money' => 0,
            'auth' => 0
        ];

        $source = model('business.Source')->where(['name' => ['LIKE', '%商城%']])->find();

        if ($source) {
            $data['sourceid'] = $source['id'];
        }

        $result = $this->BusinessModel->validate('common/business/Business.register')->save($data);

        if ($result === false) {
            $this->error($this->BusinessModel->getError());
        } else {
            $this->success('注册成功');
        }
    }

    public function login()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $password = $this->request->param('password', '', 'trim');

        // 判断用户是否存在
        $business = $this->BusinessModel->where(['mobile' => $mobile])->find();

        if (!$business) {
            $this->error('用户不存在!');
        }

        // 判断密码是否正确
        $password = md5($password . $business['salt']);
        if ($password !== $business['password']) {
            $this->error('密码错误!');
        }

        // 封装返回数据
        $data = [
            'id' => $business['id'],
            'mobile' => $business['mobile'],
            // 获取器获取，实现一些特定功能
            'mobile_text' => $business['mobile_text'],
            'avatar' => $business['avatar'],
            'avatar_cdn' => $business['avatar_cdn'],
            'nickname' => $business['nickname'],
            'email' => $business['email'],
            'gender' => $business['gender'],
            'province' => $business['province'],
            'city' => $business['city'],
            'district' => $business['district'],
            'region_text' => $business['region_text'],
            'auth' => $business['auth'],
        ];

        $this->success('登陆成功!', null, $data);
    }

    public function check()
    {
        $id = $this->request->param('id', 0, 'trim');
        $mobile = $this->request->param('mobile', '', 'trim');

        $business = $this->BusinessModel->where(['id' => $id, 'mobile' => $mobile])->find();

        if (!$business) {
            $this->error("非法登录!");
        }

        // 封装返回数据
        $data = [
            'id' => $business['id'],
            'mobile' => $business['mobile'],
            // 获取器获取，实现一些特定功能
            'mobile_text' => $business['mobile_text'],
            'avatar' => $business['avatar'],
            'avatar_cdn' => $business['avatar_cdn'],
            'nickname' => $business['nickname'],
            'email' => $business['email'],
            'gender' => $business['gender'],
            'province' => $business['province'],
            'city' => $business['city'],
            'district' => $business['district'],
            'region_text' => $business['region_text'],
            'auth' => $business['auth'],
        ];

        $this->success('验证成功!', null, $data);
    }

    public function profile()
    {
        $params = $this->request->param();

        $business = $this->BusinessModel->find($params['id']);

        if (!$business) {
            $this->error('用户不存在!');
        }

        // 封装数据
        $data = [
            'id' => $params['id'],
            'nickname' => $params['nickname'],
            'email' => $params['email'],
            'gender' => $params['gender'],
        ];

        if ($params['email'] != $business['email']) {
            $data['auth'] = 0;
        }

        $code = $params['code'] ?? '';

        // 封装地区码
        if ($code) {
            $path = model('Region')->where(['code' => $code])->value('parentpath');

            $pathArr = explode(',', $path);
            $data['province'] = $pathArr[0] ?? null;
            $data['city'] = $pathArr[1] ?? null;
            $data['district'] = $pathArr[2] ?? null;
        }

        // 头像
        if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
            $res = build_upload('avatar');

            if ($res['code'] == 0) {
                $this->error($res['msg']);
            }

            $data['avatar'] = $res['data'];
        }

        // 密码修改
        $password = $params['password'] ?? '';
        if ($password) {
            $salt = build_ranstr();
            $password = md5($password . $salt);
            $data['password'] = $password;
            $data['salt'] = $salt;
        }

        // isUpdate(true)是更新,isUpdate(false)是查询
        $result = $this->BusinessModel->validate('common/business/Business.profile')->isUpdate(true)->save($data);

        if ($result === false) {
            if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file(ltrim($data['avatar'], '/')) && unlink(ltrim($data['avatar'], '/'));
            }

            $this->error($this->BusinessModel->getError());
        } else {
            if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file(ltrim($business['avatar'], '/')) && unlink(ltrim($business['avatar'], '/'));
            }

            $this->success("更新资料成功!");
        }



    }
}